package com.beeway.blackgoose.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.beeway.blackgoose.BaseApp;
import com.beeway.blackgoose.R;
import com.beeway.blackgoose.bean.ReultCodeState;
import com.beeway.blackgoose.bean.UserLoginInfo;
import com.beeway.blackgoose.constants.Config;
import com.beeway.blackgoose.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AccountMangerActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.account_brack_tv)
    private TextView accountBrackTv;
    @ViewInject(R.id.acccount_head_img)
    private ImageView acccountHeadImg;
    @ViewInject(R.id.account_namechange_tv)
    private TextView accountNamechangeTv;
    @ViewInject(R.id.account_pwdchange_tv)
    private TextView accountPwdchangeTv;
    @ViewInject(R.id.account_emailchange_tv)
    private TextView accountEmailchangeTv;
    @ViewInject(R.id.account_safe_tv)
    private TextView accountSafeTv;
    @ViewInject(R.id.account_bind_tv)
    private TextView accountBindTv;
    @ViewInject(R.id.account_phonechange_tv)
    private TextView accountPhonechangeTv;
    @ViewInject(R.id.account_exit_btn)
    private Button accountExitBtn;
    private HttpHandler<String> handler;
    private HttpUtils utils;
    private static final String TAG = "uploadImage";
    // 选择文件
    public static final int TO_SELECT_PHOTO = 3;
    static TextView txt;
    public static String picPath = null;
    private ProgressDialog progressDialog;
    public static String picName;
    private String mptime;
    private UserLoginInfo info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_manger);
        ViewUtils.inject(this);
        initView();
        accountBrackTv.setOnClickListener(this);
    }

    private void initView() {
        utils = new HttpUtils();
        info = (UserLoginInfo) getIntent().getSerializableExtra("info");
        accountBindTv.setText(Utils.phoneReplaceStart(info.getTelephone()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        mptime = sdf.format(date);
        progressDialog = new ProgressDialog(this);

    }

    @OnClick({R.id.account_exit_btn, R.id.account_phonechange_tv, R.id.account_pwdchange_tv, R.id.account_namechange_tv, R.id.account_emailchange_tv, R.id.acccount_head_img})
    public void onClic(View v) {
        switch (v.getId()) {
            case R.id.account_exit_btn:  //退出登录

                break;
            case R.id.account_phonechange_tv:
                startActivityForResult(new Intent(this, ChangeActivity.class).putExtra(Config.COUPON_TYPE, Config.CHANGE_PHONE), Config.CHANGE_PHONE_REQUST_CODE);
                break;
            case R.id.account_pwdchange_tv:
                startActivityForResult(new Intent(this, ChangeActivity.class).putExtra(Config.COUPON_TYPE, Config.CHANGE_PWD).putExtra("info", info), Config.CHANGE_PWD_REQUST_CODE);
                break;
            case R.id.account_namechange_tv:
                startActivityForResult(new Intent(this, ChangeActivity.class).putExtra(Config.COUPON_TYPE, Config.CHANGE_NAME), Config.CHANGE_NAME_REQUST_CODE);
                break;
            case R.id.account_emailchange_tv:
                startActivityForResult(new Intent(this, ChangeActivity.class).putExtra(Config.COUPON_TYPE, Config.CHANGE_EMAIL), Config.CHANGE_EMAIL_REQUST_CODE);
                break;
            case R.id.acccount_head_img:
                Intent intent = new Intent(this, SelectPicActivity.class);
                startActivityForResult(intent, TO_SELECT_PHOTO);
                break;
        }
        Utils.getAnimIn(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Utils.getAnimOut(this);
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) {
            picPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH);
            Log.i(TAG, "最终选择的图片=" + picPath);
            // txt.setText("文件路径"+picPath);
            String[] str = picPath.split("/");
            String strr = str[str.length - 1];
            // txt.setText("文件路径" + picPath);
            picName = strr;
            BitmapFactory.Options ops = new BitmapFactory.Options();
            //ops.in
            ops.inPreferredConfig = Bitmap.Config.RGB_565;
            ops.inPurgeable = true;
            ops.inInputShareable = true;
            Bitmap bm = BitmapFactory.decodeFile(picPath, ops);
            File f = new File(getCacheDir(), "thumbnail");
            acccountHeadImg.setImageBitmap(bm);
        } else if (resultCode == Config.CHANGE_NAME_RESULT_CODE && requestCode == Config.CHANGE_NAME_REQUST_CODE) {
        } else if (resultCode == Config.CHANGE_PHONE_RESULT_CODE && requestCode == Config.CHANGE_PHONE_REQUST_CODE) {
            accountBindTv.setText(Utils.phoneReplaceStart(data.getStringExtra(Config.CHANGE_PHONE)));
        } else if (resultCode == Config.CHANGE_EMAIL_RESULT_CODE && requestCode == Config.CHANGE_EMAIL_REQUST_CODE) {
        } else if (resultCode == Config.CHANGE_PWD_RESULT_CODE && requestCode == Config.CHANGE_PWD_REQUST_CODE) {
        }
    }

    //用户退出登录
    public void userExit(String sessionid) {
        handler = utils.send(HttpRequest.HttpMethod.POST, Config.USER_EXIT_URL, Config.getUserExit(sessionid), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                if (result != null) {
                    ReultCodeState state = JSONObject.parseObject(result, ReultCodeState.class);
                    if (Config.STATE_SUCESS.equals(state.getResult())) {
                        Utils.getToastMessage("成功退出");
                        Intent intent = new Intent();
                        intent.putExtra("isexit", true);
                        setResult(10004, intent);
                        Utils.getAnimOut(getParent());
                        finish();
                    } else if (Config.STATE_FAIL.equals(state.getResult())) {
                        Utils.getToastMessage(state.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
}
